package com.hundsun.yr.universal.library.view.gallery;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

/**
 * 重写viewpager的onMeasure方法
 * 
 * @author tonglj
 * 
 */
public class WrapContentHeightViewPager extends ViewPager {
	private boolean isRemeasure = true;

	public WrapContentHeightViewPager(Context context) {
		super(context);
	}

	public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * 计算高度实现自适应
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if (isRemeasure) {
			int height = 0;
			for (int i = 0; i < getChildCount(); i++) {
				View child = getChildAt(i);
				child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
				int h = child.getMeasuredHeight();
				if (h > height)
					height = h;
			}
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
		}else {
			//do nothing
		}
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	public void setReMeasure(boolean isRemeasure){
		 this.isRemeasure = isRemeasure;
	}

}